package com.clstu.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

public class HashSetExercise {
    public static void main(String[] args) {
        Set set=new HashSet();
        set.add(new Employee("jack",20));
        set.add(new Employee("mary",20));
        set.add(new Employee("jack",20));
        for(Object o:set){
            System.out.println(o);
        }
        Iterator iterator=set.iterator();
        while (iterator.hasNext()) {
            Object next =  iterator.next();//iterator执行一次next()之后就会指向next（）
            System.out.println(next);
        }
    }
}
class Employee{
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);//不重写hashCode()方法不会再同一个table的对于位置下面
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age && employee.name.equals(name);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}